package cn.jt.springdemo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/students/myget")
public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String radio = request.getParameter("radio");
        String age = request.getParameter("age");
        System.out.println(name+radio+age);
        //存入数据库
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbv.Driver");
            //2.连接数据库
            String url = "jdbc:mysql:///cgb2110?characterEncoding=utf-8";
            Connection c = DriverManager.getConnection(url, "root", "root");
            //3.准备sql语句 获取传输器
            String sql = "insert into users values (null,?,?,?)";
            PreparedStatement p = c.prepareStatement(sql);
            //4.执行sql语句

            p.setObject(1,name);
            p.executeUpdate();

            //5.结果
            System.out.println("恭喜");
            p.close();
            c.close();

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
